package com.bwie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bwie.utils.R;
import com.bwie.utils.TokenUtil;
import com.bwie.vo.PageInfoVo;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;
import java.util.List;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bwie.pojo.Prod;
import com.bwie.mapper.ProdMapper;
import com.bwie.service.ProdService;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;

/**
 * @Author: 鑫仔
 * @Date: 2025/7/22 09:45
 * @Description:
 **/
@Transactional
@Service
public class ProdServiceImpl extends ServiceImpl<ProdMapper, Prod> implements ProdService {

    @Autowired
    private ProdMapper prodMapper;

    @Autowired
    private HttpServletRequest request;

    @Override
    public R getProdList(PageInfoVo pageInfoVo) {
        Page<Prod> prodPage = new Page<>(pageInfoVo.getPageNum(), pageInfoVo.getPageSize());
        Page<Prod> page = prodMapper.getProdList(prodPage, pageInfoVo);
        return R.OK(page);
    }

    @Override
    public R deleteBatchProd(List<Integer> ids) {
        prodMapper.deleteBatchIds(ids);
        return R.OK();
    }

    @Override
    public R addProd(Prod prod) {

        Prod prodDb = prodMapper.selectOne(new LambdaQueryWrapper<Prod>().eq(Prod::getProdSpu, prod.getProdSpu()));
        if (ObjectUtils.isNotEmpty(prodDb)) {
            return R.ERROR(500, "SPU不可重复添加");
        }

        String token = request.getHeader("token");
        String userName = TokenUtil.getUserName(token);

        prod.setProdStatus(1);
//        prod.setCreateBy(userName);
//        prod.setCreateTime(new Date());

        prodMapper.insert(prod);
        return R.OK();
    }

    @Override
    public R prodInfo(Integer prodId) {
        Prod prod = prodMapper.selectById(prodId);
        return R.OK(prod);
    }
}
